package com.office.controller;

import com.office.common.Response;
import com.office.mqtt.MqttGateway;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/mqtt")
public class MqttController {

    @Resource
    private MqttGateway mqttService;

    @PostMapping("/send")
    public Response<?> send(@RequestBody String message) {
        try {
            String topic = "office/server";
            mqttService.sendToMqtt(topic, 1, message);
            return Response.success();
        } catch (Exception e) {
            return Response.error("-1", "MQTT消息发送异常");
        }
    }

}
